<template>
  <div>
    <el-row style="margin: 10px">
      <el-col>
        <el-col :span="4">
          <el-input v-model="searchName" placeholder="通过用户名搜索" clearable maxlength="10" />
        </el-col>
        <el-col :span="1" style="margin-left: 20px">
          <el-button type="primary" @click="handleSearch">搜索</el-button>
        </el-col>
        <el-col :span="1" style="margin-left: 30px">
          <el-button type="primary" @click="openDialog">添加</el-button>
        </el-col>
      </el-col>
    </el-row>
    <el-table :data="userData" style="width: 100%">
      <el-table-column label="id" width="180" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column label="密码" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.password }}</span>
        </template>
      </el-table-column>
      <el-table-column label="头像" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" style="width: 40px; height: 40px">
        </template>
      </el-table-column>
      <el-table-column label="注册时间" width="240px" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px"><el-date-picker type="datetime" :value="scope.row.createTime" readonly /></span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <!--          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)" type="text">编辑</el-button>-->
          <el-button v-if="scope.row.isBan === false" size="mini" type="text" style="color: #F6416C" @click="handleBan(scope.$index, scope.row)">封禁</el-button>
          <el-button v-else-if="scope.row.isBan === true" size="mini" type="text" @click="handleCancelBan(scope.$index, scope.row)">解封</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-col :span="12" :offset="14" style="margin-top: 20px">
      <el-pagination
        background
        layout="prev, pager, next, jumper, sizes"
        :current-page="page.currentPage"
        :total="page.total"
        :pager-count="5"
        :page-size="page.pageSize"
        :page-sizes="page.pageSizes"
        @current-change="pageChange"
        @size-change="sizeChange"
      />
    </el-col>
    <el-dialog title="添加管理员" :visible.sync="dialogVisible" width="30%" :rules="rules">
      <el-form ref="addForm" :model="addForm" :rules="rules">
        <el-form-item label="账号：" prop="username">
          <el-col :span="18">
            <el-input v-model="addForm.username" maxlength="20" />
          </el-col>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-col :span="18">
            <el-input v-model="addForm.password" maxlength="20" />
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm('addForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAdminList, banAdmin, cancelBanAdmin, addAdmin } from '@/api/adminManage'

export default {
  data() {
    return {
      userData: [],
      searchName: '',
      dialogVisible: false,
      page: {
        pageSize: 5,
        pageSizes: [5, 10, 20, 40, 80],
        totalPage: 1,
        total: 0,
        currentPage: 1
      },
      addForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入管理员名称', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入管理员密码', trigger: 'blur' },
          { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  methods: {
    getList() {
      getAdminList({
        page: this.page.currentPage,
        limit: this.page.pageSize,
        searchName: this.searchName
      }).then((res) => {
        this.userData = JSON.parse(res.data.list)
        this.page.total = res.data.total
        this.page.totalPage = res.data.totalPage
      })
    },
    handleSearch() {
      this.currentPage = 1
      this.getList()
    },
    handleEdit(index, row) {
      console.log(index, row)
    },
    pageChange(page) {
      this.page.currentPage = page
      this.getList()
    },
    sizeChange(size) {
      this.page.pageSize = size
      this.getList()
    },
    handleBan(index, row) {
      console.log(row)
      banAdmin(row).then((res) => {
        if (res.data) {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.getList()
        } else {
          this.$message({
            message: res.message,
            type: 'error'
          })
        }
      })
    },
    handleCancelBan(index, row) {
      console.log(row)
      cancelBanAdmin(row).then((res) => {
        if (res.data) {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.getList()
        } else {
          this.$message({
            message: res.message,
            type: 'error'
          })
        }
      })
    },
    openDialog() {
      this.dialogVisible = true
    },
    handleConfirm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addAdmin(this.addForm).then((res) => {
            if (res.data) {
              this.$message({
                message: res.message,
                type: 'success'
              })
              this.getList()
              this.resetForm()
            } else {
              this.$message({
                message: res.message,
                type: 'error'
              })
              this.resetForm()
            }
            this.dialogVisible = false
          })
        } else {
          this.$message({
            message: '格式错误',
            type: 'error'
          })
          return false
        }
      })
    },
    resetForm() {
      this.addForm.username = ''
      this.addForm.password = ''
    }
  }
}
</script>

<style scoped>

</style>
